package com.pj.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.current.satoken.AuthConst;
import com.pj.project.model.mapper.SpGoodsWarehouseMapper;
import com.pj.project.model.pojo.SpGoodsWarehouse;
import com.pj.project.service.SpGoodsWarehouseService;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * GoodsWarehouseController
 * 仓库控制层
 * @author hanWu
 * 2022-12-23 18:20
 */
@RestController
@RequestMapping("/warehouse")
public class GoodsWarehouseController {

    @Resource
    SpGoodsWarehouseService spGoodsWarehouseService;

    @Resource
    SpGoodsWarehouseMapper spGoodsWarehouseMapper;

    @GetMapping("/success")
    public AjaxJson success() {
        return AjaxJson.getSuccess();
    }

    /**
     * 查看所有仓库信息
     *
     * @return
     */
    @PostMapping("/list")
    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson list() {
        SoMap so = SoMap.getRequestSoMap();
        List<SpGoodsWarehouse> list = spGoodsWarehouseMapper.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);

    }

    @PostMapping
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson add(SpGoodsWarehouse warehouse) {
        warehouse.setCreateTime(new Date());
        try {
            spGoodsWarehouseService.save(warehouse);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.getError("添加失败,检查仓库名是否重复");
        }

        return AjaxJson.getSuccess("添加成功",warehouse);

    }

    /**
     * 删除
     */
    @DeleteMapping
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson del(Integer id) {
        if (spGoodsWarehouseService.del(id)) {
            return AjaxJson.getSuccess("删除成功");
        } else {
            return AjaxJson.getError("删除失败");
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson updateById(SpGoodsWarehouse warehouse) {
        System.out.println(warehouse);
        int result = spGoodsWarehouseMapper.updateById(warehouse);
        if (result > 0) {
            return AjaxJson.getSuccess("修改成功");
        } else {
            return AjaxJson.getError("修改失败");
        }
    }

    /**
     * 根据id查询仓库名字
     */
    @PostMapping("/selectById")
    //    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson selectName(Long id) {
        if (id != null) {
            String s = spGoodsWarehouseMapper.selectByIdForName(id);
            return AjaxJson.getSuccess("查询成功",s);
        }else{
            return AjaxJson.getError("未传入id");
        }
    }

}
